//
//  LBSingleImagePicker.m
//  LandCredit
//
//  Created by Mr.Xr on 2017/4/14.
//  Copyright © 2017年 联壁. All rights reserved.
//

#import "LBSingleImagePicker.h"

@interface LBSingleImagePicker () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, weak) UIViewController *targetController;
@property (nonatomic, copy) void (^completionHandler)(UIImage *image);
@end

@implementation LBSingleImagePicker

- (void)pickImageInTargetController:(UIViewController *)controller completionHandler:(void (^)(UIImage *image))completion {
    [controller.view endEditing:YES];
    self.targetController  = controller;
    self.completionHandler = completion;
    UIAlertController *alertController = [[UIAlertController alloc] init];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *takePicture = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self showImagePickerWithType:UIImagePickerControllerSourceTypeCamera];
    }];
    UIAlertAction *choosePhoto = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self showImagePickerWithType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
    }];
    [alertController addAction:cancel];
    [alertController addAction:takePicture];
    [alertController addAction:choosePhoto];
    [self.targetController presentViewController:alertController animated:YES completion:nil];
}

- (void)showImagePickerWithType:(UIImagePickerControllerSourceType)type {
    
    if ([UIImagePickerController isSourceTypeAvailable:type]) {

        UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
        pickerController.allowsEditing            = YES;
        pickerController.delegate                 = self;
        pickerController.sourceType               = type;
        [self.targetController presentViewController:pickerController animated:YES completion:nil];
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error accessing photo library!" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil];
        [alert show];
    }
}

#pragma mark - UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    [picker dismissViewControllerAnimated:YES completion:^{
        UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        // CGSize size = CGSizeMake(ceil(self.iconImageView.frame.size.width), ceil(self.iconImageView.frame.size.height));
        //         self.iconImageView.image = [CustomImage imageWithOrigin:editedImage size:size];
        if (self.completionHandler) {
            self.completionHandler(editedImage);
        }
    }];
}

@end
